class Solution:
    def myAtoi(self, s: str) -> int:
        n = len(s)
        res = [] 
        flag1 = flag2 = False # 1表示是否已经开始输入数字，2 表示是否存在正负号
        for x in s:
            if x == " ":
                if flag1:
                    break
                if flag2:
                    break
                pass
            elif x == "+":
                if flag1:
                    break
                if flag2:
                    break
                flag2 = True
                pass
            elif x == "-":
                if flag1:
                    break
                if flag2:
                    break
                flag2 = True
                res.append(x)
            elif "0"<=x<="9":
                res.append(x)
                flag1 = True
            else: # 字母
                break
        if res == [] or (len(res) == 1 and res[0] =="-"):
            return 0
        ans = int("".join(res))
        if ans > 2**31-1:
            return 2**31-1
        elif ans < (-2)**31:
            return (-2)**31
        else:
            return ans